﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Web;
/// <summary>
/// Summary description for Sh
/// </summary>
namespace Shut
{

	public class SiteSettings
	{

		private static SiteSettings siteSettingsSingleton;


		private SiteSettings()
		{
			Load();
		}

		public static SiteSettings Instance
		{
			get
			{
				if (siteSettingsSingleton == null)
				{
					siteSettingsSingleton = new SiteSettings();
				}
				return siteSettingsSingleton;
			}
		}
		public bool DebugMode { get; set; }
		public string JqueryCDN { get; set; }
		public string JqueryUICDN { get; set; }
		public string JqueryMobileCDN { get; set; }
		
		public string SiteMapName { get; set; }
		public string SiteMinWidth {get; set;}
		public string ErrorLog { get; set; }

		public string HostUrl { get; set; }
		public string SiteTitle { get; set; }

		public string Localization_TimeZoneId { get; set; }
		public string Localization_CultureName { get; set; }
		public string Localization_TimeZoneDaylightName { get; set; }
		public string Localization_TimeZoneStandardName { get; set; }
		public string Site_CultureName { get; set; }
		public string StorageLocation { get; set; }
		public string CssLocation { get; set; }
		public string JsLocation { get; set; }
		public string DevLocation {get; set;}
		
		
		public string Twitter_ConsumerKey { get; set; }
		public string Twitter_ConsumerSecret { get; set; }
		public string Twitter_DefaultProfileImage { get; set; }
		public string Site_Domain { get; set; }
		public int LiveVersion { get; set; }
		public string SiteDirection { get; set; }
		public string GetPropertyByName(string s)
		{
			// return evaluation of property
			s = s.Replace(".", "_");
			PropertyInfo pi = this.GetType().GetProperty(s);
			if (pi != null)
			{
				return pi.GetValue(this, null).ToString();
			}
			return "";

		}
		private void Load()
		{
			Type settingsType = this.GetType();

			//------------------------------------------------------------
			//	Enumerate through individual settings nodes
			//------------------------------------------------------------

			// read configuration section 
			NameValueCollection AppSettings = (NameValueCollection)ConfigurationManager.GetSection("appSettings");

			foreach (string key in AppSettings.AllKeys)
			{
				
				string name = key.ToString();
				name = name.Replace(".", "_");

				//------------------------------------------------------------
				//	Enumerate through public properties of this instance
				//------------------------------------------------------------
				foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
				{
					//------------------------------------------------------------
					//	Determine if configured setting matches current setting based on name
					//------------------------------------------------------------
					if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
					{
						//------------------------------------------------------------
						//	Attempt to apply configured setting
						//------------------------------------------------------------
						try
						{
							if (propertyInformation.CanWrite)
							{
								propertyInformation.SetValue(this, Convert.ChangeType(AppSettings[key], propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
							}
						}
						catch
						{
							// walaishee
						}
						break;
					}
				}
			}

		}



	}

	


}